package com.huonan.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 批量读取文件
 */
public class FileUtils2 implements AutoCloseable {

    private final BufferedReader reader;
    private final int linesPerRead;

    public FileUtils2(String filePath, int linesPerRead) throws IOException {
        this.linesPerRead = linesPerRead;
        reader = new BufferedReader(new FileReader(filePath));
    }

    public List<String> readNextLines() throws IOException {
        String[] lines = new String[linesPerRead];
        for (int i = 0; i < linesPerRead; i++) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            lines[i] = line;
        }
        return Arrays.stream(lines).filter(Objects::nonNull).collect(Collectors.toList());
    }

    public void close() throws IOException {
        reader.close();
    }
}